package com.jrtc.backend.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jrtc.base.entity.bo.Foodrecord;
import com.jrtc.service.FoodrecordService;
import com.jrtc.base.config.dto.ResultDTO;
import com.jrtc.base.config.dto.ResultDTOBuilder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * 患者饮食记录表(Foodrecord)表控制层
 *
 * @author makejava
 * @since 2022-03-21 17:19:03
 */
@RestController
@RequestMapping("foodrecord")
public class FoodrecordController {
    /**
     * 服务对象
     */
    @Resource
    private FoodrecordService foodrecordService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Foodrecord selectOne(Integer id) {
        return this.foodrecordService.queryById(id);
    }
    
    @RequestMapping("getFoodrecordList")
    public ResultDTO getFoodrecordList(@RequestBody Foodrecord foodrecord)
    {
        IPage<Foodrecord> ipage = foodrecordService.queryFoodrecordList(foodrecord);
        return ResultDTOBuilder.success(ipage);
    }

    @RequestMapping("insertFoodrecord")
    public ResultDTO insertFoodrecord(@RequestBody Foodrecord foodrecord)
    {
        Foodrecord tmpFoodrecord = foodrecordService.insert(foodrecord);
        if (tmpFoodrecord != null)
        {
           return ResultDTOBuilder.success("添加成功！");
        }
        return ResultDTOBuilder.failureMsg("添加失败！");
    }

    @RequestMapping("getFoodrecordById")
    public ResultDTO getFoodrecordById(@RequestBody Map<String,Integer> map)
    {
        int foodrecordId = map.get("foodrecordId");
        Foodrecord foodrecord = foodrecordService.queryById(foodrecordId);
        if (foodrecord==null)
        {
            return ResultDTOBuilder.failureMsg("信息有误！");
        }
        return ResultDTOBuilder.success(foodrecord);
    }

    @RequestMapping("updateFoodrecord")
    public ResultDTO updateFoodrecord(@RequestBody Foodrecord foodrecord)
    {
        if (foodrecord==null)
        {
            return ResultDTOBuilder.failureMsg("信息有误！");
        }
        if (foodrecordService.update(foodrecord)!=null)
        {
            return ResultDTOBuilder.success("修改信息成功！");
        }
        return ResultDTOBuilder.failureMsg("修改信息失败！");
    }
    
    @RequestMapping("deleteFoodrecord")
    public ResultDTO deleteFoodrecord(@RequestBody Map<String,Integer> map)
    {
        int foodrecordId = map.get("foodrecordId");
        if (foodrecordService.deleteById(foodrecordId))
        {
            return ResultDTOBuilder.success("删除成功！");
        }
        return ResultDTOBuilder.failureMsg("删除失败！");
    }
}